#include <stdio.h>
#include <cgic.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

#define MAXSIZE 4396 //冥冥之中自有7意
#define N 5

char *checkbox[]={"c","w","l","a","s"};

int cgiMain()
{
    int choices[6];
    int i=0,j=0,k=0;
    int invalid;
    int number;
    char filename[128];
    char checkboxname[128];
    char buf[MAXSIZE];
    FILE *fp;
    char c;
    cgiHeaderContentType("text/html");
    cgiFormInteger("number",&number,0);
    sprintf(buf,"./wc ");
    j=strlen(buf);
    while(i<number)
    {
        i++;
        sprintf(checkboxname,"checkbox%d",i);
        sprintf(filename,"file%d",i);
        if(cgiFormCheckboxMultiple(checkboxname,checkbox,N,choices,&invalid)==cgiFormSuccess)
        {
            sprintf(&buf[j++],"-");
            for(k=0;k<N;k++)
            {
                if(choices[k])
                sprintf(&buf[j++],"%s",checkbox[k]);
            }
            sprintf(&buf[j++]," ");
        }
        if (cgiFormTempFileName(filename, filename, sizeof(filename)) !=cgiFormSuccess) { 
            fprintf(cgiOut,"could not retrieve filename<br>");    
        }
        else 
        {
            sprintf(&buf[j],"%s ",filename);
        }
        j=strlen(buf);
    }
    fprintf(cgiOut,"%s<br>",buf);
    fp=popen(buf,"r");
    if(fp!=NULL)
    {
        while(!feof(fp))
        {
            c=fgetc(fp);
            if(c=='\n'||c==-1)fprintf(cgiOut,"<br>");
            else fprintf(cgiOut,"%c",c);
        }
    }
    pclose(fp);
    return 0;
}
